package com.data.security;

import com.data.security.control.Ventana;
import com.data.security.modelo.ControlPin;
import com.data.security.modelo.Preferencias;
import com.data.security.modelo.sebastian.ShaCrypt;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

public class LoginActivity extends Activity {

	private EditText uno, dos, tres, cuatro;
	private ImageView adver;
	private ControlPin pin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actividad_login);

		cargarViews();

		pin = new ControlPin(this, uno, dos, tres, cuatro);
	}

	public void loguearse(View view) {
		adver.setVisibility(View.INVISIBLE);

		String pass = pin.getText();

		byte[] clave = ShaCrypt
				.generarArreglo(pass.getBytes(), ShaCrypt.SHA256);
		pass = Base64.encodeToString(clave, 0, clave.length, Base64.NO_WRAP);

		if (pass.equals(Preferencias.getClave(getApplicationContext()))) {
			//Preferencias.setRegistro(getApplicationContext(), true);
			activar();
		} else {
			new Ventana(this, getString(R.string.app_name),
					getString(R.string.incorrecto));
			adver.setVisibility(View.VISIBLE);
		}

	}

	
	private void activar() {
		Intent inten = new Intent(getApplicationContext(), InicioActivity.class);
		startActivity(inten);
		this.finish();
	}

	private void cargarViews() {
		uno = (EditText) findViewById(R.id.field_password1);
		dos = (EditText) findViewById(R.id.field_password2);
		tres = (EditText) findViewById(R.id.field_password3);
		cuatro = (EditText) findViewById(R.id.field_password4);
		adver = (ImageView) findViewById(R.id.advertenciaimage);
		adver.setVisibility(View.INVISIBLE);
	}

}
